﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWord.Fundamental.Data.Repositories;
using uNhAddIns.Adapters;
using XWord.Fundamental.Domain.Model;
using Luna.NHibernate;

namespace XWord.Fundamental.Domain.Impl
{
    [PersistenceConversational(MethodsIncludeMode = MethodsIncludeMode.Implicit)]
    public class WordModel : IWordModel
    {
        private readonly IWordRepository _wordRepository;
        private readonly ICategoryRepository _categoryRepository;
        private readonly IRepository<Sentence> _sentenceRepository;
        private readonly IRepository<RCArticle> _articleRepository;
        public WordModel(IWordRepository wordRepository, ICategoryRepository categoryRepository, 
            IRepository<Sentence> sentenceRepository,IRepository<RCArticle> articleRepository)
        {
            _wordRepository = wordRepository;
            _categoryRepository = categoryRepository;
            _sentenceRepository = sentenceRepository;
            _articleRepository = articleRepository;
        }

        public IList<RCArticle> GetArticleList()
        {
            return _articleRepository.GetAll();
        }

        #region IWordModel Members

        public int GetUnKnownWordCount()
        {
            return _wordRepository.GetUnKnownWordCount();
        }

        [PersistenceConversation(ConversationEndMode = EndMode.CommitAndContinue)]
        public void SaveWord(Word word)
        {
            foreach (Sentence item in word.Sentences)
            {
                _sentenceRepository.MakePersistent(item);
            }
            word.UpdateTime = DateTime.Now;
            _wordRepository.MakePersistent(word);
        }

         [PersistenceConversation(ConversationEndMode = EndMode.CommitAndContinue)]
        public void DeleteWord(Word word)
        {
            foreach (Sentence item in word.Sentences)
            {
                _sentenceRepository.MakeTransient(item);
            }
            word.UpdateTime = DateTime.Now;
            _wordRepository.MakeTransient(word);
        }

        public IList<Word> LoadWordList()
        {
           return _wordRepository.GetAll();
        }

        public IList<Category> GetCategoryList()
        {
            return _categoryRepository.GetAll();
        }

        #endregion
    }


}
